package com.szakul.pdf.model;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import java.util.List;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import com.szakul.pdf.utl.Utilities;
import com.vaadin.terminal.StreamResource.StreamSource;

public class PdfModel {


	private String fileName;
	private PDFFile document = null;
	private List<PDFPage> pages = null;
	private BufferedImage image = null;
	private int currentPage;
	private TextHighlighter textHighligter;

	private String textRepository;
	
	public PdfModel(final String fileName) {
		
		this.fileName = fileName;
		textHighligter = new TextHighlighter(fileName);
	}
	



	private PDFFile getPdfDocument() throws IOException {
		if (document == null) {
			File file = new File(fileName);
			RandomAccessFile raf = new RandomAccessFile(file, "r");
			FileChannel channel = raf.getChannel();
			ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0,
					channel.size());
			document = new PDFFile(buf);

		}
		
		return document;
	}
	
	
	private List<PDFPage> getPages() throws IOException {
		if (pages == null) {

			int pageNum = getPdfDocument().getNumPages();
			for (int i = 0; i < pageNum; i++) {
				pages.add(getPdfDocument().getPage(i));
			}

		}

		return pages;
	}
	
	public int getNumperOfPages() {
		try {
			return getPdfDocument().getNumPages();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
		return 0;
	}
	

	public List<Integer> getSearchPages(String searchWorld) throws IOException {

		return textHighligter.getMatchPages(searchWorld);

	}
	
	private BufferedImage getImage(int pageNumber, Zoom zoom) throws IOException {
		PDFPage page = getPdfDocument().getPage(pageNumber);
		int zo = 2;
		int plus_h = 0;
		int plus_w = 0;
        //get the width and height for the doc at the default zoom 
        Rectangle rect = new Rectangle(0,0,
                (int)page.getBBox().getWidth(),
                (int)page.getBBox().getHeight());
        
        
		switch (zoom) {

		case ZOOM_150:
			plus_h = 7*rect.height/10;
			plus_w = 7*rect.width/10;
			zo = 2;
			break;
		
		
		case ZOOM_140:
			plus_h = 6*rect.height/10;
			plus_w = 6*rect.width/10;
			zo = 2;
			break;
		
		case ZOOM_130:
			plus_h = 5*rect.height/10;
			plus_w = 5*rect.width/10;
			zo = 2;
			break;
		
		case ZOOM_120:
			plus_h = 4*rect.height/10;
			plus_w = 4*rect.width/10;
			zo = 2;
			break;
		
		case ZOOM_110:
			plus_h = 2*rect.height/10;
			plus_w = 2*rect.width/10;
			zo = 2;
			break;
		
		case ZOOM_100:
			zo = 2;
			break;
			
		case ZOOM_50:
			zo = 1;
			break;
		}		
        
        
        //generate the image
        Image img = page.getImage(
                zo*rect.width + plus_w, zo*rect.height+plus_h, //width & height
                rect, // clip rect
                null, // null for the ImageObserver
                true, // fill background with white
                true  // block until drawing is done
                );
		
		
		image = Utilities.toBufferedImage(img);
		
		return image;
	}
	
	public StreamSource getPage(int pageNumber) throws IOException {
		
		currentPage = pageNumber;
		
		return new PdfImageSource(getImage(pageNumber, Zoom.ZOOM_100));

	}
	
	public StreamSource getPage(int pageNumber, String text) throws IOException {

		image = getImage(pageNumber, Zoom.ZOOM_100);
		
		BufferedImage resBuf = textHighligter.highlightPage(image, pageNumber, text);
		return new PdfImageSource(resBuf);

	}


	
	public StreamSource zoomCurrentPage(Zoom zoom) throws IOException {
		return new PdfImageSource(getImage(currentPage, zoom));
	}
	
	public void closeDocument() throws IOException {

	}

	public String getFileName() {
		return fileName;
	}
	
	public BufferedImage highlightPage(String text) {
		

		return null;

	}




	public int getWidth() {
		
		return image.getWidth();
	}




	public int getHeight() {
		
		return image.getHeight();
	}
	
}
